package com.example.web.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.api.Service;
import com.example.common.po.Po;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public abstract class BaseController<T extends Po> {

    public static final int PAGECOUNT = 10;

    public abstract Service getService();

    public Map selectListMap() {
        return null;
    }

    @RequestMapping("/save")
    public String save(T model) {
        Po po = getService().selectById(model.getId());
        if (po != null) {
            model.recalculation();
            getService().updateById(model);
        } else {
            model.recalculation();
            getService().insert(model);
        }
        return model.getId();
    }

    @RequestMapping("/getById/{id}")
    public T getById(@PathVariable String id) {
        return (T) getService().selectById(id);
    }

    @RequestMapping("/delById/{id}")
    public Integer delById(@PathVariable String id) {
        return getService().deleteById(id);
    }

    @RequestMapping("/getList")
    public List<T> getList() {
        return getService().selectByMap(selectListMap());
    }

    @RequestMapping("/getPage")
    public IPage<T> getPage(@RequestParam Integer pageNum, @RequestParam Integer pageCount) {
        return getService().selectPage(pageNum, pageCount, selectPage());
    }

    protected Wrapper selectPage() {
        return null;
    }

}
